extends LabelWithFont

class_name Blinker

var active = true

const BLINK_PERIOD = 1
var V_OFFSET = 40

var waiting = false
var action_to_wait_for
var timer
var n

signal action_pressed


func _init(_text,action = null):

	visible = false

	font_path = "res://fonts/Now FL/Now-SemiBold.otf"
	font_size = 25

	text = _text
	if action:
		action_to_wait_for = action
		waiting = true

func _process(delta):
	if waiting:
		if Input.is_action_just_pressed(action_to_wait_for):
			emit_signal("action_pressed")
			self_destruct()

func _ready():
	timer = Timer.new()
	timer.wait_time = BLINK_PERIOD
	timer.connect("timeout",self,'on_timeout')
	add_child(timer)
	timer.start()
	n = Node2D.new()
	n.z_index = 6
	n.z_as_relative = false
	get_parent().add_child(n)
	get_parent().remove_child(self)
	n.add_child(self)
	rect_position = Vector2(get_viewport().size.x/2.0 - rect_size.x/2.0, V_OFFSET)

func deactivate():
	active = false
	visible = false

func activate():
	active = true

func on_timeout():
	if active:
		visible = !visible

func self_destruct():
	print("self-destruction of blinker with text ", text)
	n.queue_free()
